<!--
 * @Descripttion: (报警信息配置/cks_alarm_message_config)
 * @Author: (admin)
 * @Date: (2025-09-08)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item :label="$t('alarm.cksalarmmsgconfigPage.page.alarmContent')">
        <el-input v-model="queryParams.content" :placeholder="$t('alarm.cksalarmmsgconfigPage.page.alarmContentInput')"
          clearable style="width:240px" />
      </el-form-item>

      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>

    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['cksalarmmessageconfig:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" :disabled="single" v-hasPermi="['cksalarmmessageconfig:edit']" plain icon="edit"
          @click="handleUpdate">
          {{ $t('btn.edit') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['cksalarmmessageconfig:delete']" plain icon="delete"
          @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-dropdown trigger="click" v-hasPermi="['cksalarmmessageconfig:import']">
          <el-button type="primary" plain icon="Upload">
            {{ $t('btn.import') }}<el-icon class="el-icon--right"><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="upload">
                <importData templateUrl="business/CksAlarmMessageConfig/importTemplate"
                  importUrl="/business/CksAlarmMessageConfig/importData" @success="handleFileSuccess"></importData>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="download" @click="handleExport"
          v-hasPermi="['cksalarmmessageconfig:export']">
          {{ $t('btn.export') }}
        </el-button>
      </el-col>
      <!-- <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar> -->
    </el-row>

    <el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
      highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column prop="id" :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.id')" width="100" align="center"
        v-if="columns.showColumn('id')" />
      <el-table-column prop="plcID" :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.plcId')" width="120"
        align="center" v-if="columns.showColumn('plcID')" />
      <el-table-column prop="blockAddr" :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.blockAddr')" width="120"
        align="center" v-if="columns.showColumn('blockAddr')" />
      <el-table-column prop="alarmAddr" :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.alarmAddr')" width="150"
        align="center" v-if="columns.showColumn('alarmAddr')" />
      <el-table-column prop="alarmBitIndex" :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.alarmBitIndex')"
        width="150" align="center" v-if="columns.showColumn('alarmBitIndex')" />
      <!-- <el-table-column prop="level" :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.blockAddr')" width="100" align="center" v-if="columns.showColumn('level')"/> -->
      <el-table-column prop="messageCn" :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.messageCn')" align="center"
        :show-overflow-tooltip="true" v-if="columns.showColumn('messageCn')" />
      <el-table-column prop="messageEs" l:label="$t('alarm.cksalarmmsgconfigPage.tableColumns.messageEs')"
        align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('messageEs')" />
      <el-table-column prop="messageUs" :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.messageUs')" align="center"
        :show-overflow-tooltip="true" v-if="columns.showColumn('messageUs')" />
      <el-table-column prop="creatDate" :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.creatDate')"
        :show-overflow-tooltip="true" v-if="columns.showColumn('creatDate')" />
      <el-table-column :label="$t('btn.operate')" width="200" align="center">
        <template #default="scope">
          <el-button type="success" size="small" icon="edit" v-hasPermi="['cksalarmmessageconfig:edit']"
            @click="handleUpdate(scope.row)">{{ $t("btn.edit") }}</el-button>
          <el-button type="danger" size="small" icon="delete" v-hasPermi="['cksalarmmessageconfig:delete']"
            @click="handleDelete(scope.row)">{{ $t("btn.delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />


    <!-- 添加或修改报警信息配置对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="150px">
        <el-row :gutter="20">

          <!-- <el-col :lg="12" v-if="opertype != 1">
            <el-form-item label="id" prop="id">
              <el-input-number v-model.number="form.id" controls-position="right" placeholder="请输入报警记录ID"
                :disabled="true" />
            </el-form-item>
          </el-col> -->

          <el-col :lg="12">
            <el-form-item label="PlcID" prop="plcID">
              <el-input v-model.number="form.plcID" :placeholder="$t('alarm.cksalarmmsgconfigPage.formDialog.plcId')" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.blockAddr')" prop="blockAddr">
              <el-input v-model.number="form.blockAddr" :placeholder="$t('alarm.cksalarmmsgconfigPage.formDialog.blockAddr')"  />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.alarmAddr')" prop="alarmAddr">
              <el-input v-model.number="form.alarmAddr" :placeholder="$t('alarm.cksalarmmsgconfigPage.formDialog.alarmAddr')"  />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.alarmBitIndex')" prop="alarmBitIndex">
              <el-input v-model.number="form.alarmBitIndex" :placeholder="$t('alarm.cksalarmmsgconfigPage.formDialog.alarmBitIndex')" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.level')" prop="level">
              <!-- <el-input v-model.number="form.level" placeholder="请输入报警级别" /> -->
              <el-select style="width: 450px;" v-model="form.level" :placeholder="$t('alarm.cksalarmmsgconfigPage.formDialog.level')" >
                <el-option v-for="item in options.levelOptions" :key="item.dictValue" :label="item.dictLabel"
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="24">
            <el-form-item :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.messageCn')" prop="messageCn">
              <el-input type="textarea" v-model="form.messageCn" :placeholder="$t('alarm.cksalarmmsgconfigPage.formDialog.messageCn')"  />
            </el-form-item>
          </el-col>

          <el-col :lg="24">
            <el-form-item :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.messageEs')" prop="messageEs">
              <el-input type="textarea" v-model="form.messageEs" :placeholder="$t('alarm.cksalarmmsgconfigPage.formDialog.messageEs')"  />
            </el-form-item>
          </el-col>

          <el-col :lg="24">
            <el-form-item :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.messageUs')"  prop="messageUs">
              <el-input type="textarea" v-model="form.messageUs" :placeholder="$t('alarm.cksalarmmsgconfigPage.formDialog.messageUs')" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('alarm.cksalarmmsgconfigPage.tableColumns.creatDate')" prop="creatDate">
              <el-date-picker v-model="form.creatDate" type="datetime" :teleported="false"
              :placeholder="$t('alarm.cksalarmmsgconfigPage.formDialog.creatDate')" ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="cksalarmmessageconfig">
import {
  listCksAlarmMessageConfig,
  addCksAlarmMessageConfig, delCksAlarmMessageConfig,
  updateCksAlarmMessageConfig, getCksAlarmMessageConfig,
}
  from '@/api/business/cksalarmmessageconfig.js'
import importData from '@/components/ImportData'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  content: '',
  sort: '',
  sortType: 'asc',
})
const columns = ref([
  { visible: true, prop: 'id', label: '报警记录ID' },
  { visible: true, prop: 'plcID', label: 'PlcID' },
  { visible: true, prop: 'blockAddr', label: '数据块地址' },
  { visible: true, prop: 'alarmAddr', label: '报警地址' },
  { visible: true, prop: 'alarmBitIndex', label: '报警位索引' },
  { visible: true, prop: 'level', label: '报警级别' },
  { visible: true, prop: 'messageCn', label: '中文语报警信息' },
  { visible: false, prop: 'messageEs', label: '西班牙语报警信息' },
  { visible: true, prop: 'messageUs', label: '英文语报警信息' },
  { visible: false, prop: 'creatDate', label: '创建时间' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [

]


function getList() {
  loading.value = true
  listCksAlarmMessageConfig(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1
  multiple.value = !selection.length;
}

// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    plcID: [{ required: true, message: proxy.$t('alarm.cksalarmmsgconfigPage.formDialogVerify.plcId'), trigger: "blur" }],
    blockAddr: [{ required: true, message:  proxy.$t('alarm.cksalarmmsgconfigPage.formDialogVerify.blockAddr'), trigger: "blur" }],
    alarmAddr: [{ required: true, message:  proxy.$t('alarm.cksalarmmsgconfigPage.formDialogVerify.alarmAddr'), trigger: "blur" }],
    alarmBitIndex: [{ required: true, message:  proxy.$t('alarm.cksalarmmsgconfigPage.formDialogVerify.alarmBitIndex'), trigger: "blur" }],
    level: [{ required: true, message:  proxy.$t('alarm.cksalarmmsgconfigPage.formDialogVerify.level'), trigger: "blur" }],
    messageCn: [{ required: true, message:  proxy.$t('alarm.cksalarmmsgconfigPage.formDialogVerify.messageCn'), trigger: "blur" }],
  },
  options: {
    levelOptions: [
      { dictLabel: proxy.$t('common.general'), dictValue: '1' },
      { dictLabel: proxy.$t('common.warning'), dictValue: '2' },
      { dictLabel: proxy.$t('common.danger'), dictValue: '3' }
    ],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    plcID: null,
    blockAddr: null,
    alarmAddr: null,
    alarmBitIndex: null,
    level: null,
    messageCn: null,
    messageEs: null,
    messageUs: null,
    creatDate: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value =  proxy.$t('alarm.cksalarmmsgconfigPage.formDialog.addTitle')
  form.value.creatDate = new Date()
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getCksAlarmMessageConfig(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = proxy.$t('alarm.cksalarmmsgconfigPage.formDialog.editTitle')
      opertype.value = 2
      data.level = data.level + "";
      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateCksAlarmMessageConfig(form.value).then((res) => {
          proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
          open.value = false
          getList()
        })
      } else {
        addCksAlarmMessageConfig(form.value).then((res) => {
          proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
          open.value = false
          getList()
        })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm(proxy.$t("common.deleteConfirm",[Ids]), proxy.$t("common.systemTips"), {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delCksAlarmMessageConfig(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
    })
}


// 导入数据成功处理
const handleFileSuccess = (response) => {
  const { item1, item2 } = response.data
  var error = ''
  item2.forEach((item) => {
    error += item.storageMessage + ','
  })
  proxy.$alert(item1 + '<p>' + error + '</p>', '导入结果', {
    dangerouslyUseHTMLString: true
  })
  getList()
}

// 导出按钮操作
function handleExport() {
  proxy
        .$confirm(proxy.$t("common.exportConfirm"), proxy.$t("common.systemTips"), {
            confirmButtonText: proxy.$t('common.ok'),
            cancelButtonText: proxy.$t('common.cancel'),
            type: "warning",
        })
    .then(async () => {
      await proxy.downFile('/business/CksAlarmMessageConfig/export', { ...queryParams })
    })
}


handleQuery()
</script>